package org.kolonitsky.alexey.example
{

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.DropShadowFilter;
    import flash.text.TextField;
    import flash.text.TextFormat;

    import flashx.textLayout.formats.TextAlign;

    public class ExampleBase extends Sprite
    {
        public function ExampleBase()
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            stage.addEventListener(Event.RESIZE, resizeHandler);
        }

        protected var grid:Sprite = null;
        protected var hint:String = "";
        protected var tfHint:TextField = null;
        protected var sHintContainer:Sprite;

        protected function createHint():void
        {
            tfHint = new TextField();
            tfHint.autoSize = TextAlign.LEFT;
            tfHint.defaultTextFormat = new TextFormat("_sans", 12, 0xFFFFFF);
            tfHint.selectable = false;
            tfHint.multiline = true;
            tfHint.text = hint;
            tfHint.filters = [ new DropShadowFilter(3, 90, 0x000000) ];

            sHintContainer = new Sprite();
            sHintContainer.graphics.beginFill(0x000000, 0.8);
            sHintContainer.graphics.drawRect(0, 0, stage.stageWidth, 50);
            sHintContainer.graphics.endFill();
            sHintContainer.addChild(tfHint);

            addChild(sHintContainer)
        }

        protected function createGrid():void
        {
            var w:Number = stage.stageWidth;
            var h:Number = stage.stageHeight;
            var dx:Number = 100;
            var dy:Number = 100;

            grid.graphics.clear();
            grid.graphics.lineStyle(1, 0xEEEEEE, 0.4);
            grid.mouseChildren = false;
            grid.mouseEnabled = false;

            for (var x:Number = 0; x <= w; x += dx)
            {
                grid.graphics.moveTo(x, 0);
                grid.graphics.lineTo(x, h);
            }

            for (var y:Number = 0; y <= h; y += dy)
            {
                grid.graphics.moveTo(0, y);
                grid.graphics.lineTo(w, y);
            }

            addChildAt(grid, 1);
        }

        protected function addedToStageHandler(event:Event):void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            grid = new Sprite();
            createGrid();
            createHint();

            resizeHandler(null);
        }

        protected function resizeHandler(event:Event):void
        {
            sHintContainer.x = 0;
            sHintContainer.y = stage.stageHeight - 50;
            createGrid();
        }
    }
}
